package com.liukai.novel_base.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.liukai.novel_base.R;
import com.liukai.novel_commons.utils.State;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 所有fragment的父fragment
 */
public abstract class BaseFragment extends Fragment {

    private State currentState = State.NONE;
    private Unbinder mBind;
    private FrameLayout mBaseContainer;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;

    /**
     * 创建布局
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.base_fragment_layout, container, false);
        mBaseContainer = rootView.findViewById(R.id.base_container);
        loadStatusView(inflater, container);
        initDate(rootView);
        getFragmentContext(rootView);
        initView(rootView);
        initAdapter(rootView);
        initListener();
//        initAdapter(view);

        return rootView;
    }

    /**
     * 加载fragment外面的空壳布局
     *
     * @param inflater
     * @param container
     */
    private void loadStatusView(LayoutInflater inflater, ViewGroup container) {
        // 加载成功的View
        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);
        // 加载loading的view
        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);
        // 加载出错误页面
        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);
        // 内容为空的页面（可以提示管理员添加数据）
        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);
        setUpState(State.NONE);
    }

    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error, container, false);
    }

    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    /**
     * 子类通过该方法修改页面的状态
     * @param state
     */
    public void setUpState(State state) {
        currentState = state;
        mSuccessView.setVisibility(currentState == State.SUCCESS ? View.VISIBLE : View.GONE);
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(currentState == State.ERROR ? View.VISIBLE : View.GONE);
        mEmptyView.setVisibility(currentState == State.EMPTY ? View.VISIBLE : View.GONE);
    }

    /**
     * 加载loading界面
     *
     * @param inflater
     * @param container
     * @return
     */
    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    /**
     * 初始化数据
     */
    protected void initDate(View view) {

    }

    protected abstract Context getFragmentContext(View view);

    /**
     * 设置适配器
     */
    protected void initAdapter(View view) {

    }

    /**
     * 设置监听
     */
    protected void initListener() {

    }

    /**
     * 绑定组件
     *
     * @param view
     */
    protected void initView(View view) {
        mBind = ButterKnife.bind(this, view);
    }

    /**
     * 返回一个布局
     *
     * @param inflater
     * @param container
     * @return
     */
    protected View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(getLayoutId(), container, false);
    }

    /**
     * 获取布局的ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null) {
            mBind = null;
        }
        release();
    }

    protected void release() {
    }
}
